package com.stormragetech.teamwork.spring.controller.web;

import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.common.model.ReturnMsg;
import com.stormragetech.teamwork.common.constant.Constants;
import com.stormragetech.teamwork.module.user.model.LoginModel;
import com.stormragetech.teamwork.module.user.model.RegitrationModel;
import com.stormragetech.teamwork.module.user.spring.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
@Controller
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        Assert.notNull(userService, "UserService cannot be null !!!");
        this.userService = userService;
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(String errMsg, Model model) {

        System.out.println();
        log.info("[ Controller ] ------ login ------ [ GET ] ------ start ");

        if (null != errMsg && errMsg.length() > 0) {
            model.addAttribute(Constants.RETURN_MSG, errMsg);
        }
        return "user/login";

    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(LoginModel model, HttpServletResponse response, RedirectAttributes attr) {

        System.out.println();
        log.info("[ Controller ] ------ login ------ [ POST ] ------ start ");

        //TODO: decrypt password

        try {

            String token = userService.login(model);
            attr.addFlashAttribute(Constants.RETURN_MSG, new ReturnMsg(Constants.RETURN_SUCCESS_CODE, ""));

            // save token to cookie
            //
            Cookie cookie = new Cookie(Constants.COOKIE_NAME, token);
            cookie.setMaxAge(30 * 60); // 设置为30min
            cookie.setPath("/");
            response.addCookie(cookie);

            log.info("登录成功");
            return "redirect:/index";

        } catch (StormragetechException e) {

            if (Constants.USER_NOEXIST_CODE.equals(e.getCode()) || Constants.PASS_ERR_CODE.equals(e.getCode())) {
                attr.addFlashAttribute(Constants.RETURN_MSG, new ReturnMsg(Constants.RETURN_FAILED_CODE, e.getMessage()));
            } else {
                attr.addFlashAttribute(Constants.RETURN_MSG, new ReturnMsg(Constants.RETURN_FAILED_CODE, Constants.SERVER_ERR));
            }
            log.info("登录失败 || " + e.getMessage());
            return "redirect:/user/login";
        }
    }

    @RequestMapping(value = "/registration", method = RequestMethod.GET)
    public String registration(String errMsg, Model model) {

        System.out.println();
        log.info("[ Controller ] ------ registration ------ [ GET ] ------ start ");

        if (null != errMsg && errMsg.length() > 0) {
            model.addAttribute(Constants.RETURN_MSG, errMsg);
        }
        return "user/registration";

    }

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public String registration(RegitrationModel model, HttpServletResponse response, RedirectAttributes attr) {

        System.out.println();
        log.info("[ Controller ] ------ registration ------ [ POST ] ------ start ");

        try {

            // execute login
            // get token
            //
            String token = userService.registration(model);
            log.info("user's login token is: {}", token);

            // save token to cookie
            //
            Cookie cookie = new Cookie(Constants.COOKIE_NAME, token);
            cookie.setMaxAge(30 * 60); // 设置为30min
            cookie.setPath("/");
            response.addCookie(cookie);

            return "redirect:/index";

        } catch (StormragetechException e) {

            return "redirect:/user/registration";

        }
    }
}
